
/** this do-file creates a 'naive' version of the health variables. **/ 

/** it feeds forwards and backwards undisputed reports of diagnoses **/

	
discard

 use "$healthsave\with problem indicators_w7.dta", clear

set more off 
preserve 
  
	keep idauniq ever* disp* inwave* alive* 
	ren inwave inwave_all 
	ren *_w? *? 
	
	* makes a list of the ever had and dispute variables *
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
		local foreshape `foreshape' ever`x' disp`x' 
	}
	
	reshape long `foreshape' inwave alive , i(idauniq) j(wave) 

	** feed forward positive reports UNLESS there is a dispute 
	sort idauniq wave
	
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
		
		replace ever`x' = 1 if ever`x'[_n-1] == 1 & (disp`x'<3 | disp`x'==.) ///
								& idauniq==idauniq[_n-1] & alive==1 & wave>=2 
	
	}
	
	* feed backward a report of never having had condition if report is missing in earlier wave *
	gsort idauniq -wave 
	
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
	
		replace ever`x' = 0 if ever`x'[_n-1] == 0 & ever`x'==. ///
								& idauniq==idauniq[_n-1] & alive==1 
	
	} 
	
	sort idauniq wave 
	foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
		
		di in red "all interviewees" 
		ta wave ever`x' if inwave==1 & ever`x'!=-1 , row nof 
		
		di in red "balanced panel" 
		ta wave ever`x' if inwave_all == 17 & ever`x'!=-1 , row nof 
	} 	
	
	reshape wide 
	ren *? *_w?
	drop inwave* disp* 
	ren idauni* idauniq   
	
	
	** rename everything (grrr reshaping) 
	
	tokenize LungDisease Asthma Arthritis Osteoporosis Cancer Parkinsons ///
				 PsychiatricProblems Alzheimers Dementia BloodDisorder 		 ///
				 Hypertension Angina HeartAttack HeartFailure HeartMurmur	 ///
				 HeartRhythm Diabetes Stroke Cholesterol	
		
		foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
			forv i = 0/7 { 
				lab var ever`x'_w`i' "Reports diagnosis of `1'" 
			}
		mac shift 
		}	
	
	save "$healthsave\naive healthvars_w7.dta", replace 

restore 

drop ever* 
merge 1:1 idauniq using "$healthsave\naive healthvars_w7.dta", assert(3) nogen 
replace everch_w1 = . 

* save "$healthsave\basic healthvars.dta", replace 
	

		/** for documentation **/
	
		preserve 
		
		cd "P:\ELSA\Healthdynamics\documentation\tables\" 

		local foreshape 
		local fortabout
		local fortabout2 
		
		foreach x in lu as ar os ca pd ps ad de bl bp an mi hf hm hr di st ch { 
			forv i = 1/7 { 
				replace ever`x'_w`i' = . if inwave`i'==0 
				gen missing`x'_w`i' = ever`x'_w`i'<0 
				replace ever`x'_w`i' = . if ever`x'_w`i'<0 
				local forkeep `forkeep' ever`x'_w`i' missing`x'_w`i' 
			} 
			
		local foreshape `foreshape' ever`x' missing`x'
		local fortabout `fortabout' mean ever`x' 
		local fortabout2 `fortabout2' sum missing`x' 	
			
		}
		
		keep idauniq inwave* `forkeep'
		ren *_w? *?
		ren inwave inwaveall 
		reshape long `foreshape' inwave , i(idauniq) j(wave) 
		
		tabout wave using naive_prevalence_rates_by_wave.txt , ///
			c(`fortabout') sum replace f(4) 
			
		tabout wave if inwaveall==16 ///
			using naive_prevalence_rates_by_wave_balanced_panel.txt , ///
			c(`fortabout') sum replace f(4) 
		
		tabout wave if inwaveall==16 /// 
			using number_missings_naive.txt , /// 
			c(`fortabout2') sum replace f(0) 
		
		
		tsset idauniq wave 
		gen newan = everan - L.everan 
		replace newan = 0 if newan <0 
		gen tmp = inwave==1 & L.inwave==1 
		gen newmi = evermi - L.evermi 
		replace newmi = 0 if newmi<0 
		tabout wave if tmp == 1 using naive_onset_rates_by_wave_w7.txt , /// 
			c(mean newan mean newmi count newmi) f(4) sum replace 
		tabout wave if tmp == 1 & inwaveall==16 using naive_onset_rates_by_wave_balanced_panel_w7.txt , /// 
			c(mean newan mean newmi count newmi) f(4) sum replace 			
		
		restore 
	
	save "$healthsave\basic healthvars_w7.dta", replace
